from calendar import monthrange

import django_filters
from rest_framework.pagination import PageNumberPagination

from cost.models import *


class UserFilter(django_filters.rest_framework.FilterSet):
    # 实现过滤
    # 前端传来的参数名 = django_filters.CharFilter(model中的字段)
    # 如果两边字段一致，则右边可以不写
    username = django_filters.CharFilter(lookup_expr='icontains')
    # contains , gt , gte, lt, lte

    user_status = django_filters.CharFilter('user_status', method='filter_is_status')

    class Meta:
        model = User
        fields = []

    # 自定义过滤状态方法 queryset为所有对象， name为前端传来的字段名， value为传来的字段值
    def filter_is_status(self, queryset, name, value):
        if value in ['no', '0', 'No', 'False', 'false', 'N', 'n', 'F']:
            return queryset.filter(user_status=0)
        else:
            return queryset.filter(user_status=1)


class AccountFilter(django_filters.rest_framework.FilterSet):
    account_status = django_filters.CharFilter(method='account_is_status')

    class Meta:
        model = Account
        fields = []

    def account_is_status(self, queryset, name, value):
        if value in ['1', '活跃']:
            return queryset.filter(account_status=1)
        else:
            return queryset.filter(account_status=0)



class DetailFilter(django_filters.rest_framework.FilterSet):

    detail_type = django_filters.CharFilter(method='detail_type_method')

    class Meta:
        model = Details
        fields = []

    def detail_type_method(self, queryset, name, value):
        if value in ['1', '支出']:
            return queryset.filter(detail_type=1)
        else:
            return queryset.filter(detail_type=0)


class CustomPagePagination(PageNumberPagination):
    """自定义页码分页类"""
    page_size = 5
    page_size_query_param = 'size'
    max_page_size = 50


# 给年月，返回日期，格式为2020-02-02
def allDays(y, m):
    return ['{:02d}'.format(d) for d in range(1, monthrange(y, m)[1] + 1)]
    # return ['{:04d}-{:02d}-{:02d}'.format(y, m, d) for d in range(1, monthrange(y, m)[1] + 1)]
